package com.hb.trans.mvp.ui.activity


import android.annotation.SuppressLint
import android.os.Bundle
import android.view.MenuItem
import com.alibaba.android.arouter.facade.annotation.Route
import com.basiclib.base.BaseActivity
import com.basiclib.view.SnackbarUtil
import com.hb.trans.R
import com.hb.trans.global.ACTIVITY_HOME_FEEDBACK
import com.hb.trans.mvp.contract.FeedbackContract
import com.hb.trans.mvp.presenter.FeedbackPresenter
import com.jakewharton.rxbinding2.view.clicks
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_feedback.*
import java.util.regex.Pattern
import javax.inject.Inject

/**
 * 意见反馈
 */
@Route(path = ACTIVITY_HOME_FEEDBACK)
class FeedbackActivity : BaseActivity(), FeedbackContract.View {


    @Inject
    lateinit var mPresenter: FeedbackPresenter

    override fun getContentViewResId(): Int = R.layout.activity_feedback

    override fun onActivityInject() {
        AndroidInjection.inject(this)
    }

    override fun attachView(savedInstanceState: Bundle?) {
        mPresenter.attachView(this)
//        ImmersionBar.with(this)
//            .statusBarColor(R.color.colorPrimary)
//            .init()
    }

    override fun getPresenter(): FeedbackPresenter? = mPresenter

    override fun initView() {
        setSupportActionBar(feedbackToolbar.toolbar)
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        supportActionBar!!.setHomeAsUpIndicator(R.mipmap.ic_left_arrow)

    }

    override fun initData() {

    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> onBackPressed()
        }
        return true
    }


    @SuppressLint("CheckResult")
    override fun setListener() {
//        feedbackToolbar.backClick()?.subscribe {
//            hideSoftKeyword()
//            finish()
//        }
        btnCommitFeedback.clicks().subscribe {
            val content = etFeedback.text.toString().trim()
            val phone = etFeedback.text.toString().trim()
            when {
                phone.isNotBlank() && !checkPhoneNum(phone) -> showShortToast("请输入正确的联系方式", SnackbarUtil.ALERT)
                content.isNullOrBlank() -> showShortToast("请输入意见或问题",SnackbarUtil.ALERT)
                content.length > 100 -> showShortToast("意见或问题字数不能超过100字",SnackbarUtil.ALERT)
                else -> mPresenter.commitFeedback(etFeedback.text.toString().trim())
            }
        }
    }

    override fun commitSucess(info: String?) {
        showShortToast(info ?: "")
        finish()
    }

    /***
     * 手机号码检测
     */
    fun checkPhoneNum(num: String): Boolean {
        val regExp = "^((13[0-9])|(15[^4])|(18[0-9])|(17[0-8])|(14[5-9])|(166)|(19[8,9])|)\\d{8}$"
        val p = Pattern.compile(regExp)
        val m = p.matcher(num)
        return m.matches()
    }

    override fun onDestroy() {
        super.onDestroy()
//        ImmersionBar.with(this).destroy()
    }
}
